<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="queryParams.phonenumber"
            placeholder="请输入手机号码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户性别" prop="sex">
          <el-select v-model="queryParams.sex" placeholder="请选择客户性别" clearable>
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['manage:ptchildtable:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['manage:ptchildtable:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['manage:ptchildtable:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['manage:ptchildtable:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="ptchildtableList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户id" align="center" prop="customerId" />
        <el-table-column label="客户姓名" align="center" prop="customerName" />
        <el-table-column label="手机号码" align="center" prop="phonenumber" />
        <el-table-column label="客户性别" align="center" prop="sex">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="客户生日" align="center" prop="birthday" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="客户描述" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['manage:ptchildtable:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['manage:ptchildtable:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改主子表测试对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="客户姓名" prop="customerName">
            <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
          </el-form-item>
          <el-form-item label="客户性别" prop="sex">
            <el-select v-model="form.sex" placeholder="请选择客户性别">
              <el-option
                v-for="dict in dict.type.sys_user_sex"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="客户生日" prop="birthday">
            <el-date-picker clearable
              v-model="form.birthday"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择客户生日">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="客户描述" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-divider content-position="center">商品信息</el-divider>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSysGoods">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSysGoods">删除</el-button>
            </el-col>
          </el-row>
          <el-table :data="sysGoodsList" :row-class-name="rowSysGoodsIndex" @selection-change="handleSysGoodsSelectionChange" ref="sysGoods">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>
            <el-table-column label="商品名称" prop="name" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.name" placeholder="请输入商品名称" />
              </template>
            </el-table-column>
            <el-table-column label="商品重量" prop="weight" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.weight" placeholder="请输入商品重量" />
              </template>
            </el-table-column>
            <el-table-column label="商品价格" prop="price" width="150">
              <template slot-scope="scope">
                <el-input v-model="scope.row.price" placeholder="请输入商品价格" />
              </template>
            </el-table-column>
            <el-table-column label="商品时间" prop="date" width="240">
              <template slot-scope="scope">
                <el-date-picker clearable v-model="scope.row.date" type="date" value-format="yyyy-MM-dd" placeholder="请选择商品时间" />
              </template>
            </el-table-column>
            <el-table-column label="商品种类" prop="type" width="150">
              <template slot-scope="scope">
                <el-select v-model="scope.row.type" placeholder="请选择商品种类">
                  <el-option label="请选择字典生成" value="" />
                </el-select>
              </template>
            </el-table-column>
          </el-table>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listPtchildtable, getPtchildtable, delPtchildtable, addPtchildtable, updatePtchildtable } from "@/api/manage/ptchildtable";
  
  export default {
    name: "Ptchildtable",
    dicts: ['sys_user_sex'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 子表选中数据
        checkedSysGoods: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 主子表测试表格数据
        ptchildtableList: [],
        // 商品表格数据
        sysGoodsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          phonenumber: null,
          sex: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询主子表测试列表 */
      getList() {
        this.loading = true;
        listPtchildtable(this.queryParams).then(response => {
          this.ptchildtableList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          customerId: null,
          customerName: null,
          phonenumber: null,
          sex: null,
          birthday: null,
          remark: null
        };
        this.sysGoodsList = [];
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.customerId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加主子表测试";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const customerId = row.customerId || this.ids
        getPtchildtable(customerId).then(response => {
          this.form = response.data;
          this.sysGoodsList = response.data.sysGoodsList;
          this.open = true;
          this.title = "修改主子表测试";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.sysGoodsList = this.sysGoodsList;
            if (this.form.customerId != null) {
              updatePtchildtable(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPtchildtable(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const customerIds = row.customerId || this.ids;
        this.$modal.confirm('是否确认删除主子表测试编号为"' + customerIds + '"的数据项？').then(function() {
          return delPtchildtable(customerIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 商品序号 */
      rowSysGoodsIndex({ row, rowIndex }) {
        row.index = rowIndex + 1;
      },
      /** 商品添加按钮操作 */
      handleAddSysGoods() {
        let obj = {};
        obj.name = "";
        obj.weight = "";
        obj.price = "";
        obj.date = "";
        obj.type = "";
        this.sysGoodsList.push(obj);
      },
      /** 商品删除按钮操作 */
      handleDeleteSysGoods() {
        if (this.checkedSysGoods.length == 0) {
          this.$modal.msgError("请先选择要删除的商品数据");
        } else {
          const sysGoodsList = this.sysGoodsList;
          const checkedSysGoods = this.checkedSysGoods;
          this.sysGoodsList = sysGoodsList.filter(function(item) {
            return checkedSysGoods.indexOf(item.index) == -1
          });
        }
      },
      /** 复选框选中数据 */
      handleSysGoodsSelectionChange(selection) {
        this.checkedSysGoods = selection.map(item => item.index)
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('manage/ptchildtable/export', {
          ...this.queryParams
        }, `ptchildtable_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
  